home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12173 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  96 lines

  1. Path: news.cc.sunysb.edu!ghauser
  2. From: ghauser@ic.sunysb.edu (George Hauser)
  3. Newsgroups: comp.lang.c
  4. Subject: Need help with a STRING that won't go away!!!
  5. Date: 29 Mar 1996 17:42:26 GMT
  6. Organization: State University of New York at Stony Brook
  7. Message-ID: <4jh7e2$jjr@abel.cc.sunysb.edu>
  8. NNTP-Posting-Host: libws4.cc.sunysb.edu
  9.  
  10.  
  11. Hi... 
  12.  
  13. I have the following problem. This program is supposed to do
  14. some text formating so that we can bring data into our database
  15. without problems.
  16.  
  17. Tha part works fine... it may not be the best way (I always get new 
  18. suggestions) but it works Ok.
  19.  
  20. Now, the great pain in the butt is what follows:
  21.  
  22. In this program I am interested in processing unti the EOF, when
  23. I get to it I don't want to loop again to avoid dumping the
  24. data that still is in the string (line).
  25.  
  26. As it is, it dumps the exact same record that was on the last line twice!
  27. This causes the import to POST an OCEAN FREIGHT amount 2x!!!
  28.  
  29. This is really bad. 
  30.  
  31. So what I did as a quick fix was to reset the  line to be full of NULLS
  32.  
  33. I figured this do nothing.  fputs with nulls should write nothing....
  34.  
  35. WRONG it is writting an extra line (or at least a EOL (\n)).
  36.  
  37. This causesxs another problem.... we import a empty record
  38. into our database which causes the sequential assignment of numbers
  39. to get all screwed up.... pretty bad also.
  40.  
  41. How in the world can I prevent t code from executing this fputs
  42. when the next char is the EOL? I remember in pascal you can look
  43. at the next char without actually moving the file pointer. Is there
  44. something like that in C? this way I could say something like.
  45.  
  46. -- If nextchar is EOL(\n) then skip the fputs
  47.  
  48. I have tried getting the lenght strlen to prevent writing if it is
  49. 0 but a line full of nulls does not return 0!!
  50.  
  51. I'm sure there is a simple way to check for this but I am stuck...
  52. and I have an ugly procedure in my database that checks for empty
  53. records... which sucks because this problem should be taken care of here
  54. before creating the new file.
  55.  
  56.  
  57. Thanks so much and I live the main() so that you know what I am talking about.
  58.  
  59. - George
  60.  
  61. (ps. Any help will be appreciated! ghauser@ic.sunysb.edu)
  62.  
  63.  
  64. #include <stdio.h>
  65. #include <string.h>
  66. #include <stdlib.h>
  67.  
  68. #define linemax 194    /* The predifined lenght of the record */
  69.  
  70. --- All previous functions and file openings are coded here.
  71.  
  72.  records = 0;
  73.  while(!feof(sourcefp))
  74.  {
  75.        if (ferror(sourcefp))
  76.        {
  77.            printf("\nError while reading source file.\n");
  78.            exit(1);
  79.        }
  80.        fgets(line,linemax,sourcefp);    /* Get Bad Record */
  81.            rmgarbage(line);            /* Remove bad chars */
  82.        fillrecords(line);            /* Format the record line */
  83.  
  84.        /* here I tried checking for empty lines */
  85.        fputs(line,destfp);            /* Write to disk */
  86.  
  87.        records++;                /* Increment our counter */
  88.  
  89.      /* here I was setting the line[] to '\0' */
  90.  
  91.  } 
  92.  fclose(sourcefp);  /* Close source file. */
  93.  fclose(destfp);    /* Close destination file. */
  94.  printf("Total Number Of Records Processed: %d \n", records);
  95. } /* End */
  96.